<?
/*
 *  GGB - Gelin's Guest Book.
 *  Copyright (C) 2006  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 *
 *  Description:    Action of third page of installation wizard.
 *
 *  History:
 *  2006-06-21  Gelin   Initially created.
 *  2008-08-02  Gelin   Now using $_SESSION and $_POST, added DB charset, removed e-mail
 */

session_start();

$location = 'step3.php';

$_SESSION['error'] = '';

$adminName = $_SESSION['adminName'] = $_POST['adminName'];
$adminPassword = $_SESSION['adminPassword'] = $_POST['adminPassword'];

//no checks

$location = 'step4.php';

//create tables
$_SESSION['sqlLog'] = '';
$dbHostname = $_SESSION['dbHostname'];
$dbUsername = $_SESSION['dbUsername'];
$dbPassword = $_SESSION['dbPassword'];
$link = @mysql_connect($dbHostname, $dbUsername, $dbPassword) or sqlError();
$dbName = $_SESSION['dbName'];
mysql_select_db($dbName, $link) or sqlError();
$queries = join('', file('create_tables.sql.tpl'));
$queries = ereg_replace('/\*.*\*/', '', $queries);  //remove comments
$dbTablePrefix = $_SESSION['dbTablePrefix'];
$queries = explode(';', $queries);
foreach ($queries as $query) {
    $query = str_replace('%dbTablePrefix%', $dbTablePrefix, $query);
    if (trim($query) == '') {
        continue;
    }
    mysql_query($query, $link) or sqlError();
    $_SESSION['sqlLog'] .= "\nSuccesfully executed query:\n$query\n";
}
mysql_close($link);

//create config
$dbCharset = $_SESSION['dbCharset'];
$config = join('', file('config.php.tpl'));
$config = ereg_replace('/\*.*\*/', '', $config);    //remove comments
$config = str_replace('%dbHostname%', $dbHostname, $config);
$config = str_replace('%dbUsername%', $dbUsername, $config);
$config = str_replace('%dbPassword%', $dbPassword, $config);
$config = str_replace('%dbName%', $dbName, $config);
$config = str_replace('%dbCharset%', $dbCharset == '' ? 'utf8' : $dbCharset, $config);
$config = str_replace('%dbCharsetComment%', $dbCharset == '' ? '//' : '', $config);
$config = str_replace('%dbTablePrefix%', $dbTablePrefix, $config);
$config = str_replace('%adminName%', $adminName, $config);
$config = str_replace('%adminPassword%', $adminPassword, $config);
$_SESSION['configFile'] = $config;

header("Location: $location");

function sqlError() {
    $_SESSION['error'] .= "<br/>mySQL: ".mysql_error();
    $location = 'step2.php';
    header("Location: $location");
    exit();
}

?>